package service;

import java.net.URI;

import org.apache.http.conn.HttpHostConnectException;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
import org.xmlrpc.android.XMLRPCFault;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.kbs.R;
import com.kbs.common.Utils;

public class XMLRPCMethod extends Thread {

	private String mMethod;
	private Object[] mParams;
	private Handler mHandler;
	private XMLRPCMethodCallback mCallBack;
	private XMLRPCClient mClient;
	private URI mUri;
	private ProgressDialog mProgressDialog;
	private Context mContext;
	private final String TAG = getClass().getSimpleName();

	public XMLRPCMethod(final Context context, String method,
			XMLRPCMethodCallback callBack) {
		mUri = URI.create(context.getString(R.string.url_server_connection));
		mClient = new XMLRPCClient(mUri);
		this.mMethod = method;
		this.mCallBack = callBack;
		this.mContext = context;
		mHandler = new Handler();
	}

	public void call() {
		if (Utils.checkInternetConnection(this.mContext))
			call(null);
		else
			Toast.makeText(mContext,
					mContext.getString(R.string.message_network_error),
					Toast.LENGTH_LONG).show();
	}

	public void call(Object[] params) {
		if (Utils.checkInternetConnection(this.mContext)) {
			System.out.println("Calling host " + mUri.getHost());
			mProgressDialog = ProgressDialog.show(mContext, "", "Loading...");
			this.mParams = params;
			start();
		} else
			Toast.makeText(mContext, R.string.message_network_error,
					Toast.LENGTH_LONG).show();
	}

	@Override
	public void run() {
		try {
			final long t0 = System.currentTimeMillis();
			final Object result = mClient.callEx(mMethod, mParams);
			final long t1 = System.currentTimeMillis();
			mHandler.post(new Runnable() {
				public void run() {
					System.out.println("XML-RPC call took " + (t1 - t0) + "ms");
					mCallBack.callFinished(result);
				}
			});
		} catch (final XMLRPCFault e) {
			mHandler.post(new Runnable() {
				public void run() {
					System.out.println("Fault message: " + e.getFaultString()
							+ "\nFault code: " + e.getFaultCode());
					Log.d(TAG, "Error: ", e);
					Toast.makeText(mContext,
							R.string.message_can_not_connect_to_server,
							Toast.LENGTH_LONG).show();
				}
			});
		} catch (final XMLRPCException e) {
			mHandler.post(new Runnable() {
				public void run() {
					Throwable couse = e.getCause();
					if (couse instanceof HttpHostConnectException) {
						System.out.println("Cannot connect to "
								+ mUri.getHost()
								+ "\nMake sure "
								+ mUri.getHost()
								+ " server on your development host is running !!!");
					} else {
						System.out.println("Error " + e.getMessage());
					}
					Toast.makeText(mContext,
							R.string.message_can_not_connect_to_server,
							Toast.LENGTH_LONG).show();
					Log.d(TAG, "Error: ", e);
				}
			});
		}
		mProgressDialog.dismiss();
	}

}
